﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
using System.Data;

namespace TotalSport
{
    public partial class EditInvoice : System.Web.UI.Page
    {

        protected void Page_Load(object sender, EventArgs e)
        {
            BAL.conn.connectionString = ConfigurationManager.ConnectionStrings["TotalSport"].ConnectionString;
            if (Session["UserId"] == null || Session["UserId"].ToString() == "")
            {
                Session.Abandon();
                Session.RemoveAll();
                Response.Redirect("LoginUser.aspx");
            }

            if (!IsPostBack)
            {
                if (Request.QueryString["refno"] != null)
                {
                    int referenceno = Convert.ToInt32(Request.QueryString["refno"]);
                    ViewState["Refno"] = referenceno;
                    lblRefNo.Text = referenceno.ToString();
                    string[] detaills = BAL.Customers.getpernldetail(referenceno);


                    lblname.Text = detaills[2] + detaills[3];
                    lblCompanyname.Text = detaills[6];
                    lblcontactno.Text = detaills[5];
                    lblemailid.Text = detaills[4];
                    lbllocation.Text = detaills[11];
                    personalid.InnerHtml = detaills[0];
                    getproduct();

                }

            }
        }

        protected void grdinvoicedtl_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            grdinvoicedtl.PageIndex = e.NewPageIndex;
            getproduct();
        }

        public void getproduct()
        {

            int refereno = Convert.ToInt32(ViewState["Refno"]);
            DataSet ds = BAL.Customers.getproducinvoicegw(refereno);

            grdinvoicedtl.DataSource = ds;
            grdinvoicedtl.DataBind();

        }

        protected void btnsave_Click(object sender, EventArgs e)
        {
            // int pernldtalid = personalid.InnerText;
            //  int subid=

            if (grdinvoicedtl.Rows.Count > 0)
            {
                foreach (GridViewRow gvRow in grdinvoicedtl.Rows)
                {

                    string brandname = gvRow.Cells[1].Text;
                    string itemcode = gvRow.Cells[2].Text;

                    string particularname = gvRow.Cells[0].Text;
                    TextBox txtbox = (TextBox)gvRow.FindControl("txtquanty");


                    Label lblprice = (Label)gvRow.FindControl("lblprice");
                    int price = Convert.ToInt32(lblprice.Text);
                    int qnty = 1; ;

                    if (txtbox.Text != "")
                    {

                        qnty = Convert.ToInt32(txtbox.Text);

                    }


                    int finalpirce = price * qnty;

                    Label lblfinal = (Label)gvRow.FindControl("lblquntyprice");
                    int personalidd = Convert.ToInt32(personalid.InnerText);
                    int subid = Convert.ToInt32(grdinvoicedtl.DataKeys[gvRow.RowIndex]["SubId"]);
                    int refno = Convert.ToInt32(lblRefNo.Text);
                    BAL.Customers.Updateinvoice(personalidd, subid, qnty, finalpirce, refno);



                    // lblfinal.Text=finalpirce;
                    // TextBox txtbx = (TextBox)gvRow.FindControl("txtfinalprice");
                    //Label lbllid = (Label)gvRow.FindControl("lblid");
                    // txtbx.Text = lbllid.Text;
                    // lbllid.Text = "hiii";

                    //  decimal finalpricetxt = Convert.ToDecimal(txtbx.Text);

                    // string loca = ddllocation.SelectedValue;



                }

            }

            spnMsg.Style.Add("display", "block");
            spnMsg.InnerHtml = "Updated Successfully";


            if (grdinvoicedtl.Rows.Count > 0)
            {
                foreach (GridViewRow gvRow in grdinvoicedtl.Rows)
                {
                    TextBox txtbox = (TextBox)gvRow.FindControl("txtquanty");

                    // txtbox.Enabled = false;
                }
            }
            // btnsave.Enabled = false;
            //  btncancel.Enabled = false;



        }

        protected void calcifnalprice(object sender, EventArgs e)
        {
            if (grdinvoicedtl.Rows.Count > 0)
            {
                foreach (GridViewRow gvRow in grdinvoicedtl.Rows)
                {

                    TextBox txtbox = (TextBox)gvRow.FindControl("txtquanty");

                    Label lblprice = (Label)gvRow.FindControl("lblprice");
                    int price = Convert.ToInt32(lblprice.Text);
                    int qnty = 1; ;

                    if (txtbox.Text != "")
                    {

                        qnty = Convert.ToInt32(txtbox.Text);

                    }


                    int finalpirce = price * qnty;

                    Label lblfinal = (Label)gvRow.FindControl("lblquntyprice");
                    lblfinal.Text = finalpirce.ToString();
                }
                // TextBox txtbx = (TextBox)gvRow.Fi
            }

        }

        protected void btncancel_Click(object sender, EventArgs e)
        {
            Response.Redirect("SelectQuotes.aspx");

        }
    }
}